﻿<%@ WebHandler Language="C#" Class="SavePhoto" %>

using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Web.SessionState;
using attendance.L3Bussobject;
using attendance.L2Busslogic;
using attendance.L4View.student;
using attendance.L3Bussobject;
using attendance.L2Busslogic;

public class SavePhoto : IHttpHandler, IRequiresSessionState
{
    public static string ID;
    public static string name;
    public static string numb;
    public static string photo;
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        if (context.Request.Form["PHeight"] != null && context.Request.Form["PWidth"] != null && context.Request.Form["strBMP"] != null)
        {
            try
            {
                int height = int.Parse(context.Request.Form["PHeight"].ToString());
                int width = int.Parse(context.Request.Form["PWidth"].ToString());
                string strBmp = context.Request.Form["strBMP"].ToString();
                string a = SaveBmp(BuildBitmap(width, height, strBmp), context.Server.MapPath("image"));
                context.Session["xxx"] = "~/L4View/student/image/" + a.Substring(62);
                context.Response.Write("RetMsg=true");
            }
            catch (Exception)
            {
                context.Response.Write("RetMsg=false");
            }
        }
        
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public System.Drawing.Bitmap BuildBitmap(int width, int height, string strBmp)
    {
        System.Drawing.Bitmap tmpBmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        string[] StmpBmp = strBmp.Split(',');
        int pos = 0;
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                tmpBmp.SetPixel(x, y, Color.FromArgb(int.Parse(StmpBmp[pos], System.Globalization.NumberStyles.HexNumber)));
                pos++;
            }
        }
        return tmpBmp;
    }
    public string SaveBmp(System.Drawing.Bitmap bmp,string filePath)
    {
        string FileName = filePath + "\\" + System.Guid.NewGuid().ToString() + ".bmp";
        bmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Bmp);
        return FileName;
    }

}